import 'dart:math';

import 'package:flutter/material.dart';
import 'package:top_foundation/utils/ex/image.dart';
import 'package:whale/helper/utils/util.dart';
import 'package:whale/pages/im/im/index.dart';
import 'package:whale/pages/im/model/msg.dart';
import 'package:whale/pages/im/util/im_define.dart';
import 'package:whale/pages/im/util/index.dart';
import 'widgets.dart';

class IMImage extends IMCell {
  const IMImage({super.key, required super.md});
  @override
  Widget build(BuildContext context) {
    ImMsgImageModel m = md.msg as ImMsgImageModel;
    var contentW = IMDefineContentConfig.maxW;
    double _w() {
      return min(m.w, contentW);
    }

    double _h() {
      if (m.w < contentW) {
        return m.h;
      }
      return contentW / m.w * m.h;
    }

    return GetBuilder<ImController>(
        id: m.id,
        builder: (_) {
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              IImage.net(
                url: m.thumb,
                width: _w(),
                height: _h(),
                fit: BoxFit.cover,
                errorBuilder: (str, error) {
                  return Container(
                    width: _w(),
                    height: _h(),
                    alignment: Alignment.center,
                    child: IconButton(
                      onPressed: () {},
                      icon: Icon(
                        Icons.refresh,
                        color: IMConfig.bubbleSelfColor,
                        size: 30.r,
                      ),
                    ),
                  );
                },
              ),
            ],
          );
        });
  }
}
